CharSheetPlus = {
	["Mode"] = 0,
	["ExtraStatFrameLeftDropDown"] = "PLAYERSTAT_BASE_STATS",
	["ExtraStatFrameRightDropDown"] = "PLAYERSTAT_BASE_STATS",
}

function CharSheet_OnEvent(self, event, addon)
	if event == "ADDON_LOADED" then
		if addon == "CharSheetPlus" then
			if CharSheetPlus.Mode == 1 then CharSheetToggle() end
		end
		if not CharSheetComp_MrDressUp and (addon == "MrDressUp" or IsAddOnLoaded("MrDressUp")) then
			CharSheetComp_MrDressUp = 1
			MrDressupFrameShowHelm:ClearAllPoints()
			MrDressupFrameShowHelm:SetPoint("BOTTOMLEFT", CharSheetButton, "TOPLEFT", -2, 0)
			MrDressupFrameShowHelmText:SetText("Helm")
			MrDressupFrameShowHelm:SetHitRectInsets(0,0,0,0)
			MrDressupFrameShowCloakText:SetText("Cloak")
			MrDressupFrameShowCloak:SetHitRectInsets(0,0,0,0)
		end
	end
end

--for 3.1
function CharSheet_OnLoad(self)
	self:RegisterEvent("ADDON_LOADED")
	CharacterShoulderSlot:SetFrameLevel(CharacterShoulderSlot:GetFrameLevel()+2)
end

--for 3.2
--[[
function CharSheet_OnLoad(self)
	self:RegisterEvent("ADDON_LOADED")
	PlayerTitlePickerFrame:SetFrameLevel(PlayerTitlePickerFrame:GetFrameLevel()+2)
end
]]

function CharSheetToggle()
	if CharSheetPlusFrame:IsShown() then
		CharSheetPlus.Mode = 0
		CharSheetPlusFrame:Hide()
		CharacterModelFrame:Show()
		CharSheetButton:ClearAllPoints()
		--CharSheetButton:SetPoint("BOTTOMLEFT", PlayerStatFrameLeftDropDown, "TOPLEFT", 15, -5)
		CharSheetButton:SetPoint("BOTTOMLEFT", PlayerStatFrameLeftDropDown, "TOPLEFT", 18, 0)
		CharSheetButton:SetNormalTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Up")
		MagicResFrame1:ClearAllPoints()
		MagicResFrame1:SetPoint("TOPLEFT", CharacterResistanceFrame, "TOPLEFT", 0, 0)
		MagicResFrame2:ClearAllPoints()
		MagicResFrame2:SetPoint("TOP", MagicResFrame1, "BOTTOM", 0, 0)
		MagicResFrame3:ClearAllPoints()
		MagicResFrame3:SetPoint("TOP", MagicResFrame2, "BOTTOM", 0, 0)
		MagicResFrame4:ClearAllPoints()
		MagicResFrame4:SetPoint("TOP", MagicResFrame3, "BOTTOM", 0, 0)
		MagicResFrame5:ClearAllPoints()
		MagicResFrame5:SetPoint("TOP", MagicResFrame4, "BOTTOM", 0, 0)
		
		--MrDressupFrameShowHelm:ClearAllPoints()
		--MrDressupFrameShowHelm:SetPoint("BOTTOMLEFT", CharSheetButton, "TOPLEFT", 0, 0)
		
	else
		CharSheetPlus.Mode = 1
		CharSheetPlusFrame:Show()
		CharacterModelFrame:Hide()
		CharSheetButton:ClearAllPoints()
		--CharSheetButton:SetPoint("BOTTOMLEFT", ExtraStatFrameLeftDropDown, "TOPLEFT", 15, -5)
		CharSheetButton:SetPoint("BOTTOMLEFT", ExtraStatFrameLeftDropDown, "TOPLEFT", 18, 0)
		CharSheetButton:SetNormalTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Up")
		MagicResFrame5:ClearAllPoints()
		MagicResFrame5:SetPoint("TOPRIGHT", CharacterResistanceFrame, "TOPRIGHT", 0, 0)
		MagicResFrame4:ClearAllPoints()
		MagicResFrame4:SetPoint("RIGHT", MagicResFrame5, "LEFT", 0, 0)
		MagicResFrame3:ClearAllPoints()
		MagicResFrame3:SetPoint("RIGHT", MagicResFrame4, "LEFT", 0, 0)
		MagicResFrame2:ClearAllPoints()
		MagicResFrame2:SetPoint("RIGHT", MagicResFrame3, "LEFT", 0, 0)
		MagicResFrame1:ClearAllPoints()
		MagicResFrame1:SetPoint("RIGHT", MagicResFrame2, "LEFT", 0, 0)
	end
end

function CharSheetDrop_OnLoad(self)
	UIDropDownMenu_Initialize(self, CharSheetDrop_Initialize)
	UIDropDownMenu_SetSelectedValue(self, CharSheetPlus[self:GetName()])
	myUIDropDownMenu_SetWidth(self, 99)
	UIDropDownMenu_JustifyText(self, "LEFT")
end

function myUIDropDownMenu_SetWidth(frame, width, padding)
	getglobal(frame:GetName().."Middle"):SetWidth(width);
	local defaultPadding = 25;
	if ( padding ) then
		frame:SetWidth(width + padding);
	else
		frame:SetWidth(width + defaultPadding);
	end
	if ( padding ) then
		getglobal(frame:GetName().."Text"):SetWidth(width);
	else
		getglobal(frame:GetName().."Text"):SetWidth(width - defaultPadding);
	end
	frame.noResize = 1;
end

function CharSheetDrop_Initialize(self)
	local info = UIDropDownMenu_CreateInfo()
	local checked
	for i=1, getn(PLAYERSTAT_DROPDOWN_OPTIONS) do
		if ( PLAYERSTAT_DROPDOWN_OPTIONS[i] == CharSheetPlus[self:GetName()] ) then
			checked = 1
		else
			checked = nil
		end
		info.text = getglobal(PLAYERSTAT_DROPDOWN_OPTIONS[i])
		info.func = CharSheetDrop_OnClick
		info.value = PLAYERSTAT_DROPDOWN_OPTIONS[i]
		info.checked = checked
		info.owner = UIDROPDOWNMENU_OPEN_MENU
		UIDropDownMenu_AddButton(info)
	end
end

function CharSheetDrop_OnClick(self)
	UIDropDownMenu_SetSelectedValue(self.owner, self.value)
	CharSheetPlus[self.owner:GetName()] = self.value
	UpdatePaperdollStats( gsub(self.owner:GetName(), "(ExtraStatFrame.+)DropDown", "%1"), self.value)
end

hooksecurefunc("PaperDollFrame_UpdateStats", function()
	UpdatePaperdollStats("ExtraStatFrameLeft", CharSheetPlus["ExtraStatFrameLeftDropDown"])
	UpdatePaperdollStats("ExtraStatFrameRight", CharSheetPlus["ExtraStatFrameRightDropDown"])
end)
